package com.tiancheng.trade.order.controller.api;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.utils.JsonUtil;
import com.tiancheng.trade.order.bo.cmq.ScrmOrder;
import com.tiancheng.trade.order.model.RefundOrdersDetail;
import com.tiancheng.trade.order.service.RefundOrdersDetailService;
import com.tiancheng.trade.order.service.RefundOrdersService;
import com.tiancheng.trade.order.vo.api.ScrmOrderQueryVO;
import com.tiancheng.trade.order.vo.api.res.RefundCreateResVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tiancheng.trade.order.vo.open.order.OpenRefundCreateResVO;
import com.tiancheng.trade.order.vo.open.order.OpenRefundVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单退款接口
 *
 * @author kellen
 * @date 2024-03-18
 */
@Slf4j
@RestController
@RequestMapping("/api/refund")
public class RefundApiController {

    @Resource
    private RefundOrdersService refundOrdersService;
    @Resource
    private RefundOrdersDetailService refundOrdersDetailService;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 查询退款订单详情
     *
     * @param requestData
     * @return
     */
    @RequestMapping("queryRefundDetailsById")
    public Result queryRefundDetailsById(@RequestParam Map<String, Object> requestData) throws JsonProcessingException {
        log.info("获取单笔退款订单 para:{}", objectMapper.writeValueAsString(requestData));
        RefundOrdersDetail order = refundOrdersDetailService.queryRefundOrderDetailsById(Long.parseLong(requestData.get("id").toString()));
        log.info("获取单笔退款订单  返回data:{}", objectMapper.writeValueAsString(order));
        return Result.success(order);
    }

    /**
     * 创建并执行退款
     * @param refundVO
     * @return
     */
    @PostMapping(value = "/execute", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result<OpenRefundCreateResVO> execute(@RequestBody @Valid OpenRefundVO refundVO) {
        OpenRefundCreateResVO resVO = refundOrdersService.createAndSubmit(refundVO);
        return Result.success(resVO);
    }

    /**
     * 功能描述: 查询退款订单
     *
     * @param:userId，refundId
     * @return:
     * @auther: LiuDong
     * @date: 9:54 2024/3/23
     */
    @GetMapping("/queryRefund")
    public Result queryRefund(@RequestParam("user_id") @NotNull String userId, @RequestParam("flow_id") @NotNull String flowId) {
        log.info("查询退款订单 userId:{},flowId:{}", userId, flowId);
        Result result = refundOrdersDetailService.queryRefund(userId, flowId);
        log.info("查询退款订单 返回data:{}", JsonUtil.toJsonString(result));
        return result;
    }

    /**
     * 根据批次号查询退款记录汇总
     * @param uid
     * @param paymentId
     * @return
     */
    @GetMapping("/getByPaymentId")
    public Result getByPaymentId(@RequestParam @NotNull String uid, @RequestParam("payment_id") @NotNull String paymentId) {
        return Result.success(refundOrdersDetailService.getByPaymentId(uid, paymentId));
    }

    @PostMapping(value ="/getScrmPayOrder")
    public Result queryPayOrderAndSubOrder(@RequestBody @Valid ScrmOrderQueryVO queryVO){
        final PageInfo<ScrmOrder> scrmOrderPageInfo = refundOrdersDetailService.queryPayOrderAndSubOrder(queryVO);
        return Result.success(scrmOrderPageInfo);
    }
}
